package com.example.boos.stores.myInfo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.login.login;
import com.example.boos.stores.money.balance_main;
import com.example.boos.stores.myInfo.updateInfo.updateInfo;

/*
* 商户个人信息页面
* */
public class MyInfoFragment extends Fragment {

    private TextView riderName;
    private TextView riderTelephone;
    private TextView myOrder;
    //private TextView myAddress;
    private TextView updateUserInfo;
    private TextView exitLogin;
    private View view;
    private ImageView userIamge;
    private TextView myMoney;
    private Activity act;
    private MyInfoFragmentCallBack callBack;

    public MyInfoFragment(Activity act,MyInfoFragmentCallBack callBack) {
        this.act = act;
        this.callBack = callBack;
    }

   /* public static MyInfoFragment newInstance() {
        return new MyInfoFragment();
    }*/

    // 更新ui操作
    public void updateView(){
        // 更新一下数据
        riderName.setText(BossInfo.boss.getBossName());
        riderTelephone.setText(BossInfo.boss.getTelephone());
        Glide.with(view).load(BossInfo.boss.getImage()).into(userIamge);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_myinfo, container, false);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //mViewModel = new ViewModelProvider(this).get(MyInfoViewModel.class);
        // TODO: Use the ViewModel
        // TODO: setListener
        initListener();
    }

    private void initView(){
        riderName =  view.findViewById(R.id.riderName);
        riderTelephone =  view.findViewById(R.id.riderTelephone);
        myOrder =  view.findViewById(R.id.myOrder);
        //myAddress =  view.findViewById(R.id.myAddress);
        updateUserInfo =  view.findViewById(R.id.updateUserInfo);
        exitLogin =  view.findViewById(R.id.exitLogin);
        userIamge = view.findViewById(R.id.UserImage);
        myMoney = view.findViewById(R.id.myMoney);

        // 设置数据
        riderName.setText(BossInfo.boss.getBossName());
        riderTelephone.setText(BossInfo.boss.getTelephone());
        Glide.with(view).load(BossInfo.boss.getImage()).into(userIamge);
    }

    private void initListener(){
        // 我的钱包
        myMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("测试","我的钱包事件");
                // 跳转到钱包页面
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(act, balance_main.class));
                act.startActivity(intent);
            }
        });
        // 订单管理
        myOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回到店铺页面。
                // 利用 fragmentManager 关闭掉当前的fragment
                callBack.gotoMyStore();
            }
        });

        // 退出登录
        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 退出登录
                SharedPreferences.Editor edit = act.getSharedPreferences("BossInfo", Context.MODE_PRIVATE).edit();
                edit.clear();
                edit.commit();
                // 跳转到登录页面
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(act, login.class));
                act.startActivity(intent);
            }
        });

        // 修改个人信息
        updateUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(act, updateInfo.class));
                act.startActivity(intent);
            }
        });
    }
}
